#pragma once
#include "simple-socket/TCPSocket.h"
#include <iostream>
#include "jacx_fifo.h"
#include <string.h>

class PcSocket
{
public:
	PcSocket(NET::TCPSocket::Handle hd):
		sock(hd)
	{
	}

	int rcv_from_sock()
	{
		uint8_t buf[1024];
		int len = 0;
		while (true)
		{
			auto sz = std::min(sizeof(buf), fifo.remain_size());
			if (!sz)
				break;
			try
			{
				auto res = sock.receive(buf, sz);
				if (res)
				{
					fifo.push_back(buf, res);
					len += res;
				}
				if (res < sz)
					break;
			}
			catch (const std::exception&e)
			{
				std::cout << "error " << e.what() << std::endl;
			}

		}
		return len;
	}

	int send_1k_data()
	{
		int res = 0;
		try
		{
			res = sock.send(sendbuf, 1024);
		}
		catch (const std::exception& e)
		{
			std::cout << "send_1k_data error " << e.what() << std::endl;
		}
		return res;
	}

public:
	NET::TCPSocket sock;
	char sendbuf[1024] = { 0 };
	jacx_fifo fifo{};
};
